{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-02T07:13:26.653402Z",
     "start_time": "2025-01-02T07:13:26.643401Z"
    }
   },
   "source": [
    "import tensorflow as tf\n",
    "import warnings\n",
    "\n",
    "\n"
   ],
   "outputs": [],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-02T07:13:26.966811Z",
     "start_time": "2025-01-02T07:13:26.945756Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 当去计算一个节点的时候，TensorFlow自动计算它依赖的一组节点，并且首先计算依赖的节点\n",
    "# 创建一个可训练的变量w，并初始化为3\n",
    "w = tf.Variable(3)\n",
    "# w = tf.constant(3)  # 如果改变为常量，这里将不再是可训练的变量\n",
    "x = w + 2  # 计算x，x依赖于w\n",
    "y = x + 5  # 计算y，y依赖于x\n",
    "z = x * 3  # 计算z，z也依赖于x\n"
   ],
   "id": "4718001ad781b7f4",
   "outputs": [],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-02T07:18:24.696815Z",
     "start_time": "2025-01-02T07:18:24.666927Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建一个TensorFlow会话\n",
    "with tf.Session() as sess:\n",
    "    sess.run(w.initializer)  # 初始化变量w\n",
    "    print(sess.run(y))  # 计算并打印y的值\n",
    "    # 这里为了计算z，TensorFlow需要重新计算x和w的值\n",
    "    # 除了Variable类型的值外，TensorFlow不会缓存其他类型的值，比如常量等\n",
    "    # 一个Variable的生命周期从它的initializer运行开始，到会话关闭时结束\n",
    "    print(sess.run(z))  # 计算并打印z的值"
   ],
   "id": "de381ee0adec7c7e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "15\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-02T07:18:24.933901Z",
     "start_time": "2025-01-02T07:18:24.919899Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 为了有效计算y和z，避免重复计算w和x两次，我们需要在一个图里计算y和z\n",
    "with tf.Session() as sess:\n",
    "    sess.run(w.initializer)  # 初始化变量w\n",
    "    y_val, z_val = sess.run([y, z])  # 一次性计算y和z的值\n",
    "    print(y_val)  # 打印y的值\n",
    "    print(z_val)  # 打印z的值"
   ],
   "id": "d1dfa30d5106dace",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "15\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-02T08:24:30.207096Z",
     "start_time": "2025-01-02T08:24:30.202128Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "a87eac1d385539ab",
   "outputs": [],
   "execution_count": 17
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "4511b9f42cb98124"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
